package com.xunshibao;

import java.io.IOException;
import java.util.ListIterator;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JavaEyeSpider {

    /**
     * @param args
     */
    public static void main(String[] args) {
        HttpClient client = new DefaultHttpClient();
        // HttpHost proxy = new HttpHost("web-proxy.china.hp.com", 8080);
        // client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
        // proxy);

        HttpGet get = new HttpGet("http://www.javaeye.com");
        get.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
        try {
            HttpResponse resp = client.execute(get);
            HttpEntity entity = resp.getEntity();
            if (entity != null) {
                long len = entity.getContentLength();
                if (len != -1) {
                    // System.out.println(EntityUtils.toString(entity));
                    String content = EntityUtils.toString(entity);
                    Document doc = Jsoup.parse(content);
                    Element e = doc.getElementById("news");
                    Elements es = e.select("a");
                    ListIterator<Element> it = es.listIterator();
                    while(it.hasNext()) {
                        Element item = it.next();
                        String title = item.attr("title");
                        String href = item.attr("href");
                        String text = item.ownText();
                        System.out.println("title: " + title + ", href=" + href + ", text=" + text);
                    }
//                    System.out.println(e.html());
                } else {
                    // Stream content out
                }
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
